package com.ruoyi.youtian.controller;

import com.ruoyi.common.annotation.AutoSetDefaultParams;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.youtian.domain.FaultRecord;
import com.ruoyi.youtian.domain.vo.FaultRecordVo2;
import com.ruoyi.youtian.domain.vo.FaultreportVO;
import com.ruoyi.youtian.service.IFaultRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 故障记录Controller
 *
 * @author ruoyi
 * @date 2022-09-07
 */
@RestController
@RequestMapping("/youtian/record")
public class FaultRecordController extends BaseController
{
    @Autowired
    private IFaultRecordService faultRecordService;

    /**
     * 查询故障记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list( FaultRecord faultRecord)
    {
        startPage();
        List<FaultRecord> list = faultRecordService.selectFaultRecordList(faultRecord);
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().forEach(record->{
                // 原recordName  "异常名称,时间",进行拆解
                String[] split = record.getRecordName().split(",");
                if (split.length==2) {
                    record.setRecordName(split[0]);
                    record.setCreateBy(split[1]);
                }

            });
        }
        return getDataTable(list);
    }


    /**
     * 查询故障记录列表
     * 功能修改添加字段
     */
//    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @RequestMapping("/reolist")
    @ResponseBody
    public TableDataInfo addlist( FaultRecord faultRecord) {
        startPage();
        List<FaultRecord> list = faultRecordService.selectFaultRecordListUpdate(faultRecord);
        return getDataTable(list);
    }

    /**
     * 查询故障记录列表
     * 功能修改添加字段
     */
//    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @RequestMapping("/miinlist")
    @ResponseBody
    public TableDataInfo dataInfo(FaultreportVO FaultreportVO) {
        startPage();
        List<FaultreportVO> list = faultRecordService.selectFaultRecordListdataInfo(FaultreportVO);
        return getDataTable(list);
    }


    /**
     * 导出故障记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultRecord faultRecord)
    {
        List<FaultRecord> list = faultRecordService.selectFaultRecordList(faultRecord);
        ExcelUtil<FaultRecord> util = new ExcelUtil<FaultRecord>(FaultRecord.class);
        util.exportExcel(response, list, "故障记录数据");
    }

    /**
     * 获取故障记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{recordId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(faultRecordService.selectFaultRecordByRecordId(recordId));
    }

    /**
     * 新增故障记录
     */
//    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult add(@RequestBody FaultRecord faultRecord)
    {
        return toAjax(faultRecordService.insertFaultRecord(faultRecord));
    }

    /**
     * 修改故障记录
     */
//    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "故障记录", businessType = BusinessType.UPDATE)
    @RequestMapping("/update")
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult edit(@RequestBody FaultRecord faultRecord)
    {
        return toAjax(faultRecordService.updateFaultRecord(faultRecord));
    }

    /**
     * 删除故障记录
     */
//    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "故障记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(faultRecordService.deleteFaultRecordByRecordIds(recordIds));
    }
}
